package com.banciyuan.bcywebview.biz.circles.smooth.fragment;

import android.os.Build;
import android.os.Bundle;
import android.text.TextUtils;

import com.android.volley.RequestQueue;
import com.banciyuan.bcywebview.base.fragment.BaseScrollFragment;
import com.banciyuan.bcywebview.base.view.foldview.FoldLayout;
import com.banciyuan.bcywebview.utils.common.UIUtils;
import com.banciyuan.bcywebview.utils.http.VolleyQueue;
import com.banciyuan.bcywebview.utils.universalimageloader.core.ImageLoader;

/**
 * Created by squall on 2016/6/13.
 */
public class CircleBaseSmoothFragment extends BaseScrollFragment implements FoldLayout.FoldChild {


    protected boolean isViewInitiated;
    protected boolean isVisibleToUser;
    protected boolean isDataInitiated;

    protected ImageLoader imageLoader;
    protected boolean mLastItemVisible = false;
    protected boolean loading = false;
    protected boolean end_flag = false;
    protected int page = 1;
    protected RequestQueue queue;
    protected FoldLayout.OnFoldScrollListener onFoldScrollListener;

    protected String name;
    protected String id;
    protected String type;

    protected int offset;

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        isViewInitiated = true;
        prepareFetchData();
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        this.isVisibleToUser = isVisibleToUser;
        prepareFetchData();
    }

    public void fetchData() {
    }

    public boolean prepareFetchData() {
        return prepareFetchData(false);
    }

    public boolean prepareFetchData(boolean forceUpdate) {
        if (isVisibleToUser && isViewInitiated && (!isDataInitiated || forceUpdate)) {
            fetchData();
            isDataInitiated = true;
            return true;
        }
        return false;
    }

    @Override
    protected void initArgs() {
        queue = VolleyQueue.getRquest(getActivity());
        Bundle args = getArguments();
        if (!TextUtils.isEmpty(args.getString("name"))) {
            name = args.getString("name");
        }
        if (!TextUtils.isEmpty(args.getString("id"))) {
            id = args.getString("id");
        }
        if (!TextUtils.isEmpty(args.getString("type"))) {
            type = args.getString("type");
        }
    }

    public int getOffset() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            return UIUtils.dip2px(20, getActivity());
        } else {
            return 0;
        }
    }

    @Override
    public void getParentHeight(int height) {

    }

    @Override
    public void passParentScrollY(FoldLayout.OnFoldScrollListener onFoldScrollListener) {
        this.onFoldScrollListener = onFoldScrollListener;
    }
}
